package com.weigang.ehcache.dao;

import com.weigang.ehcache.entity.Book;
import com.weigang.ehcache.test.MyKeyGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;


@Repository
//指明使用的缓存的名字 这个配直可选，若不使用 @CacheConfig 注解，则直接在 @Cacheable 注解中指明缓存名字
@CacheConfig(cacheNames = {"book_cache"})
public class BookDao {

//    @Autowired
//    MyKeyGenerator myKeyGenerator;

    @Cacheable
    public Book getBookById(Integer id) {
        System.out.println("getBookById");
        Book book = new Book();
        book.setId(id);
        book.setName("三国演义");
        book.setAuthor("罗贯中");
        return book;
    }

    @CachePut(key = "#book.id")
    public Book updateBookById(Book book) {
        System.out.println("updateBookById");
        book.setName("三国演义2");
        return book;
    }

    @CacheEvict(key = "#id")
    public void deleteBookById(Integer id) {
        System.out.println("deleteBookById");
    }
}


